package it.evento.controllers;

import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import it.evento.Events;
import it.evento.Location;

//CALENDAR
import com.google.api.services.calendar.model.CalendarListEntry;
import it.evento.BusinessLogic;
import it.evento.manager.CalendarManager;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;


/**
 *
 * @author Mark
 */
@WebServlet(urlPatterns = {"/CalendarController"})
public class CalendarController extends HttpServlet {

    @EJB
    private BusinessLogic businessLogic;
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        CalendarManager cm = new CalendarManager();
        //OAuthManager oam = new OAuthManager();

        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

            switch (request.getParameter("type")) {
              
                case "createEvent":     
                    //ottiene/crea il calendario EvenTO dell'utente
                    com.google.api.services.calendar.model.Calendar evento=cm.inizializeUserCalendar();
                    System.out.println(evento.getSummary());
                    
                    int id=Integer.parseInt(request.getParameter("eventPresent"));
                    Events e=businessLogic.getEvent(id);
                    /*
                    String location = request.getParameter("evLocation");
                    String start = request.getParameter("evStartDate");
                    String end = request.getParameter("evEndDate");
                    
                    Date startD=new Date();
                    Date endD=new Date();
                    try {
                        startD = new SimpleDateFormat("MM/dd/yyyy HH:mm").parse(start);
                        endD = new SimpleDateFormat("MM/dd/yyyy HH:mm").parse(end);  
                    } catch (ParseException ex) {
                       System.out.println("CalendarController - Exception: "+ex);
                    }
                   */
                    /*
                    Date startDate = new Date();
                    Date endDate = new Date(startDate.getTime() + 3600000);
                    */
                    String current_location="";
                    List<Location> list_location=businessLogic.getAllLocations();
                    for(Location l:list_location){
                        if(e.getLocation()==l.getId())
                            current_location=l.getName();
                    }
                    
                    cm.createEvent(evento,e.getName(),current_location,e.getStartdate(),e.getEnddate());
                    
                    response.sendRedirect("planning.jsp?Calendar="+evento.getId());
                    
                    break;
                /*
                case "getUserCalendars":
                    java.util.List<CalendarListEntry> items = cm.getUserCalendars();
                    for (CalendarListEntry calendarListEntry : items) {
                        System.out.println("Calendario utente: "+calendarListEntry.getSummary());
                    }
                    response.sendRedirect("index.jsp?listCalendars=ok");
                break;
                 */
                 
                /*    
                case "inizializeUserCalendar":
                    com.google.api.services.calendar.model.Calendar evento1=cm.inizializeUserCalendar();
                    System.out.println(evento1.getSummary());
                    response.sendRedirect("calendarIndex.jsp?Calendar="+evento1.getId());
                */
            }
            

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
